package pers.cz.controller;

import pers.cz.pub.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * 案例录制
 * @program: PostGirl-panent
 * @description: AutoCollectionTestCaseController
 * @author: Cheng Zhi
 * @create: 2022-02-13 20:14
 **/
@RestController
@RequestMapping(path = "/api/AutoCollectionTestCaseController")
public class AutoCollectionTestCaseController {

    @Autowired
    JedisPool jedisPool;

    /**
     * 修改录制状态
     * @param map
     * @return
     */
    @PostMapping(path = "/updateSwitch")
    public RespBean updateSwitch(@RequestBody Map map) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String key = (String) map.get("key");
            String value = (String) map.get("value");
            resource.set(key, value);
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
        return RespBean.ok("修改成功");
    }

    /**
     * 刷新过期录制限制时间
     * @param map
     * @return
     */
    @PostMapping(path = "/refreshTime")
    public RespBean refreshTime(@RequestBody Map map) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String key = (String) map.get("key");
            resource.expire(key, 1); // 设置过期时间为1，1秒后自动重新计数，同时联带计数器也从0开始
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
        return RespBean.ok("重置成功");
    }

    /**
     * 自定义刷新录制限制时间
     * @param map
     * @return
     */
    @PostMapping(path = "/refreshTimeByCustomize")
    public RespBean refreshTimeByCustomize(@RequestBody Map map) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String key = (String) map.get("key");
            Integer value = (Integer) map.get("value");
            resource.expire(key, value); // 默认过期时间为24小时
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
        return RespBean.ok("刷新成功");
    }

    /**
     * 获取剩余时间
     * @param map
     * @return
     */
    @PostMapping(path = "/getCurrentKeyExpireTime")
    public Long getCurrentKeyExpireTime(@RequestBody Map map) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String key = (String) map.get("key");
            Long ttl = resource.ttl(key);
            return ttl;
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
    }

    /**
     * 获取当前录制的总数
     * @param map
     * @return
     */
    @PostMapping(path = "/getCurrentCount")
    public Long getCurrentCount(@RequestBody Map map) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String key = (String) map.get("key");
            String s = resource.get(key);
            if (s == null) {
                return 0L;
            }
            return Long.valueOf(s);
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
    }

    /**
     * 获取当前队列积压数
     * @param map
     * @return
     */
    @PostMapping(path = "/getCurrentQueueSize")
    public Long getCurrentQueueSize(@RequestBody Map map) {

        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String key = (String) map.get("key");
            Long llen = resource.llen(key);
            return llen;
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
    }

    /**
     * 修改录制开关
     * @param map
     * @return
     */
    @PostMapping(path = "/updateCollectionSwitch")
    public RespBean updateCollectionSwitch(@RequestBody Map map) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String key = (String) map.get("key");
            Boolean value = (Boolean) map.get("value");
            if (value) {
                resource.set(key, "1");
            } else {
                resource.set(key, "0");
            }

            return RespBean.ok("修改成功");
        } finally {
            if (resource != null) {
                resource.close();
            }
        }
    }

    /**
     * 获取录制状态
     * @param map
     * @return
     */
    @PostMapping(path = "/getCollectionSwitchType")
    public boolean getCollectionSwitchType(@RequestBody Map map) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            String key = (String) map.get("key");
            String s = resource.get(key);
            if (s.equals("1")) {
                return true;
            } else {
                return false;
            }

        } finally {
            if (resource != null) {
                resource.close();
            }
        }
    }


}
